package com.aaa.controller;

import com.aaa.entity.Role;
import com.aaa.entity.User;
import com.aaa.service.RoleService;
import com.aaa.service.UserService;
import com.aaa.util.DefaultMsg;
import com.aaa.util.TableData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;

    @RequestMapping("/toList")
    public String toList(){
        return "user/list";
    }

    @RequestMapping("/listData")
    @ResponseBody
    public TableData<User> listData(@RequestParam(defaultValue = "") String keyword, Integer page, Integer limit){
        TableData<User> tableData = userService.listPage(keyword,page, limit);
        return tableData;
    }


    @RequestMapping("/toEdit")
    public String toEdit(){
        return "user/edit";
    }


    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public DefaultMsg saveOrUpdate(User user){
        DefaultMsg defaultMsg = userService.saveOrUpdate(user);
        return defaultMsg;
    }


    /**
     *
     * 校验用户名重复
     * @param username
     * @param id
     * @return
     */
    @RequestMapping("/checkUsername")
    @ResponseBody
    public DefaultMsg checkUsername(String username,Integer id){
        DefaultMsg defaultMsg = userService.checkUsername(username,id);
        return defaultMsg;
    }


    @RequestMapping("/delete")
    @ResponseBody
    public DefaultMsg delete(Integer id){
        DefaultMsg defaultMsg = userService.delete(id);
        return defaultMsg;
    }
    @RequestMapping("/toAddRole")
    public String toAddRole(){
        return "user/editRole";
    }


    @RequestMapping("/addRole")
    @ResponseBody
    public DefaultMsg addRole(User user){
        DefaultMsg defaultMsg = userService.addRole(user);
        return defaultMsg;
    }


    @RequestMapping("/checkIdRole")
    @ResponseBody
    public List<Integer> checkIdRole(Integer id){
        List<Integer> rid = roleService.checkIdRole(id);
        return rid;
    }





}
